<?php
// Start the session
@session_start();

$id = session_id();
if ($id == "") {
	exit;
}

// If folder does not exist just exit
$tmpDataFolder = "../tmp_data/" . $id;
if (!file_exists($tmpDataFolder)) {
	exit();
}

// Load landmark xml
// $xmlFile = simplexml_load_file(realpath(dirname(__FILE__) . "/../tmp_data/" . $id . "/landmarks.xml"));
$xmlFile = new DOMDocument();
$xmlFile->load(realpath(dirname(__FILE__) . "/../tmp_data/" . $id . "/landmarks.xml"));

// Missing data
$missingData = json_decode($_POST["missingData"]);
$face = $xmlFile->getElementsByTagName("face")->item(0);

foreach ($missingData as $element) {
	//Create landmark element
	$landmarkElement = $xmlFile->createElement("landmark");
	$typeIdElement = $xmlFile->createElement("type_id", $element->type_id);
	$xElement = $xmlFile->createElement("x", $element->location->x);
	$yElement = $xmlFile->createElement("y", $element->location->y);
	$landmarkElement->appendChild($typeIdElement);
	$landmarkElement->appendChild($xElement);
	$landmarkElement->appendChild($yElement);
	
	// Add to missing
	$face->appendChild($landmarkElement);
}
$xmlFile->save(realpath(dirname(__FILE__) . "/../tmp_data/" . $id . "/landmarks.xml"));
?>